<template>
  <base-modal
    size="mini"
    :title="$t('smartPlan.adjustLinkman')"
    :show.sync="visible"
    width="500px"
    class="adjust-linkman-dialog"
  >
    <div class="base-modal-content adjust-linkman-content">
      <el-form
        ref="form"
        class="form-container"
        :rules="rules"
        label-position="top"
        :model="form"
      >
        <el-form-item :label="$t('common.base.contact')" prop="linkman">
          <biz-remote-select
            ref="linkman"
            :placeholder="$t('task.tip.searchTip', {name: $t('common.base.contact')})"
            v-model="form.linkman"
            :cleared="true"
            :remote-method="searchLinkmanOuterHandler"
            @input="updateLinkman"
            :computed-width-keys="['name']"
          >
          </biz-remote-select>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{$t('common.base.cancel')}}</el-button>
      <el-button type="primary" @click="submit" :disabled="pending">{{$t('common.base.makeSure')}}</el-button>
    </div>
  </base-modal>
</template>

<script>
/* api */
import { updatePlanRangeLinkMan } from '@src/modules/smartPlan/api/index.js'
/* util */
import methods from '@src/modules/task/edit/components/TaskEditForm/methods'
import { t } from '@src/locales';

export default {
  name: 'adjust-linkman',
  props: {
    createDimension: {
      type: [String, Number],
      default: 0
    },
  },
  data() {
    return {
      visible: false,
      pending: false,
      form: {
        linkman: [],
      },
      planId: '', // 任务id
      objectId: '', // 客户/产品id
      customerId: '',
      rules: {
        linkman: [
          { required: true, message: t('common.placeholder.select'), trigger: 'change' },
        ]
      }
    }
  },
  methods: {
    ...methods.search,
    /**
    * @description 打开计划时间弹窗
    */
    openDialog(row) {
      this.visible = true
      this.planId = row.planId
      this.objectId = row.id
      this.customerId = this.createDimension == 0 ? row.customerId : row.id
      this.form.linkman = []
      this.$nextTick(() => {
        this.$refs.form?.clearValidate()
      })
    },
    /** 
     * @description 搜索联系人 外层处理器
     * @param {Object} params 搜索参数
    */
    async searchLinkmanOuterHandler(params = {}) {
      params.customerId = this.customerId || '';
      
      return this.customerId ? this.searchLinkman(params) : this.searchCustomerByPhone(params);
    },
    updateLinkman(value) {
      this.form.linkman = value
      this.$refs.form.validate()
    },
    submit() {
      this.$refs.form.validate().then(valid => {
        if (valid) {
          let linkman = this.form.linkman;
          let params = { 
            id: this.planId,
            objectId: this.objectId,
            linkmanId: linkman[0].id
          }
          
          if (this.pending) return;
          this.pending = true;
          
          updatePlanRangeLinkMan(params)
            .then(res => {
              if (res.succ) {
                this.$platform.notification({
                  title: t('common.base.tip.editSuccess'),
                  type: 'success'
                })
                this.visible = false
                this.$emit('success')
              } else {
                this.$platform.notification({
                  title: res.message,
                  type: 'error'
                });
              }
            })
            .finally(() => {
              this.pending = false
            })
        }
      })
      
    }
  }
}
</script>

<style lang="scss" scoped>
.adjust-linkman-dialog {
  .form-container {
    padding: 24px;
  }
}
</style>
